package com.uchiyou.walletwrapper.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;

import java.math.BigDecimal;

public class TxActionUtil {
    private static JsonRpcHttpClient client = ClientUtil.getClient();

    /**
     * 1. 使用当前钱包向地址转账 amount 金额
     * @param address
     * @param amount
     * @return
     */
    public static String sendtoaddress(String address,double amount) {
        String result = null;
        try {
            result = client.invoke("sendtoaddress", new Object[] {address,amount}, JsonNode.class).toString();
        }catch (Throwable e){
            e.printStackTrace();
        }
        return  result;
    }

    /**
     * 2. 查看所有地址历史入账记录
     * @return
     */
    public static String listreceivedbyaddress(){
        String result = null;
        try {
            result = client.invoke("listreceivedbyaddress", new Object[] {}, JsonNode.class).toString();
        }catch (Throwable e){
            e.printStackTrace();
        }
        return  result;
    }

    /**
     * 3. 列出最近交易信息
     * @return
     */
    public static String listtransactions(){
        String result = null;
        try {
            result = client.invoke("listtransactions", new Object[] {}, JsonNode.class).toString();
        }catch (Throwable e){
            e.printStackTrace();
        }
        return  result;
    }

    /**
     * 4 根据交易 id 获取交易信息
     * @return
     */
    public static String gettransaction(String txid){
        String result = null;
        try {
            result = client.invoke("gettransaction", new Object[] {txid}, JsonNode.class).toString();
        }catch (Throwable e){
            e.printStackTrace();
        }
        return  result;
    }


}
